package com.tiantian.quanminxiu.presenter;

import com.shenglian.utils.utils.CommonUtils;
import com.tiantian.quanminxiu.api.AnchorService;
import com.tiantian.quanminxiu.api.HeePayService;
import com.tiantian.quanminxiu.utils.ApplicationBase;
import com.tiantian.quanminxiu.utils.Constants;
import com.tiantian.quanminxiu.utils.UserData;
import com.tiantian.quanminxiu.view.RechargeView;

import org.json.JSONObject;

import java.io.IOException;

import rx.android.schedulers.AndroidSchedulers;


/**
 * Created by admin on 2016/8/11.
 */
public class RechargePresent implements Presenter<RechargeView> {

    private RechargeView rechargeView;

    @Override
    public void attachView(RechargeView view) {
        this.rechargeView = view;
    }

    @Override
    public void detachView() {
        rechargeView = null;
    }

    //22支付宝 30微信
    private String agent_bill_id = "";
    private String agentid = "";
    public void rechargeSubToXiu(int pay_type, double pay_amt, String pay_code) {
        rechargeView.Loading_Open(true);
        ApplicationBase app = ApplicationBase.get(rechargeView.getContext());
        AnchorService service = app.getAnchorService();
        service.payorder(UserData.Current.mid, UserData.Current.sign, Constants.APP_TYPE, pay_type, pay_amt, pay_code)
                .flatMap(str -> {
                    JSONObject jsonObject = str.optJSONObject("data");
                    agent_bill_id = jsonObject.optString("agent_bill_id");
                    String version = jsonObject.optString("version");
                    String return_url = jsonObject.optString("return_url");
                    String notify_url = jsonObject.optString("notify_url");
                    String user_ip = jsonObject.optString("user_ip");
                    String agent_bill_time = jsonObject.optString("agent_bill_time");
                    String goods_name = jsonObject.optString("goods_name");
                    String remark = jsonObject.optString("remark");
                    agentid = jsonObject.optString("agentid");
                    String sign = jsonObject.optString("sign");
                    return HeePayService.Factory.create().PayInit("1", pay_type + "", "2106635", agent_bill_id, pay_amt + "", return_url, notify_url, user_ip, agent_bill_time, goods_name, "1", remark,
                            "", CommonUtils.MD5("version=1&agent_id=2106635&agent_bill_id="
                                    + agent_bill_id + "&agent_bill_time=" + agent_bill_time
                                    + "&pay_type=" + pay_type + "&pay_amt=" + pay_amt
                                    + "&notify_url=" + notify_url + "&user_ip=" + user_ip
                                    + "&key=E5E55367530C4E50AE84109E"));
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(app.defaultSubscribeScheduler())
                .finallyDo(() -> {
                    rechargeView.Loading_Close();
                })
                .subscribe(str -> {
                    try {
                        String strValue = str.string();
                        if (strValue.contains("<error>")) {
                            if (strValue.length() - strValue.lastIndexOf("<error>") < 8) {
                                throw new RuntimeException("数据解析错误");
                            }
                            String error = strValue.substring(strValue.lastIndexOf("<error>") + 7, strValue.lastIndexOf("</error>"));
                            throw new RuntimeException(error);
                        } else if (strValue.contains("<token_id>")) {
                            if (strValue.length() - strValue.lastIndexOf("<token_id>") < 11) {
                                throw new RuntimeException("数据解析错误");
                            }
                            String token_Id = strValue.substring(strValue.lastIndexOf("<token_id>") + 10, strValue.lastIndexOf("</token_id>"));
                            String param = token_Id + "," + "2106635" + ","
                                    + agent_bill_id + "," + pay_type;
                            rechargeView.rechargeSubToHeePay(param);
                        }

                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }, throwable -> {
                    CommonUtils.showToask(rechargeView.getContext(), "创建订单失败：" + throwable.getMessage());
                });
    }
}